package rabbitmq.work;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import rabbitmq.constants.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.建立连接
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        factory.setUsername(Constants.USERNAME);
        factory.setPassword(Constants.PASSWORD);
        factory.setVirtualHost(Constants.VIRTUAL_HOST);

        Connection connection = factory.newConnection();
        // 2.开启信道
        Channel channel = connection.createChannel();
        // 3.声明交换机  使用内置的交换机
        // 4.声明队列
        channel.queueDeclare(Constants.WORK_QUEUE,true,false,false,null);
        // 5.发送消息
        String msg = null;
        for (int i = 0; i < 20; i++) {
            msg = "hello work queue " + i;
            channel.basicPublish("",Constants.WORK_QUEUE,null,msg.getBytes());
        }
        // 6.资源释放
        channel.close();
        connection.close();
        System.out.println("发送完毕~");
    }
}
